#include "Humans.h"
#include <QPaintEvent>
#include <QResizeEvent>
#include <QPainter>
#include "Common/MoveableItem.h"
#include "Head.h"
#include "Node.h"
#include "Join.h"
#include "Man.h"
#include "Emotion.h"
#include <QDebug>

Humans::Humans(QWidget *parent) :
	QWidget(parent)
{

	for(int i=0;i<1;i++){
		Man* man = new Man(this);
		_men.append(man);
	}
	for(int i=1;i<11;i++){
		Emotion* emotion = new Emotion(this);
		emotion->setEmotion(EmotionType(i));
		connect(emotion, SIGNAL(mouseDown()), SLOT(onEmotionDown()));
		connect(emotion, SIGNAL(mouseUp()), SLOT(onEmotionUp()));
		connect(emotion, SIGNAL(mouseMove()), SLOT(onEmotionMove()));
		_emotions.append(emotion);
	}
	btnAddMan = new BaseButton(this);
	btnRemoveMan = new BaseButton(this);
	btnExit = new BaseButton(this);
	btnExit->setImage(new QPixmap(":/images/exit_normal.png"),new QPixmap(":/images/exit_pressed.png"));
	btnAddMan->setImage(new QPixmap(":/images/arrow-right.png"));
	btnRemoveMan->setImage(new QPixmap(":/images/arrow-left.png"));

	connect(btnAddMan, SIGNAL(clicked()), SLOT(onAddMan()));
	connect(btnRemoveMan, SIGNAL(clicked()), SLOT(onRemoveMan()));
}

Humans::~Humans()
{
}

void Humans::paintEvent(QPaintEvent *event)
{
	int x = width()*415/500;
	int w = width() - x;

	QPainter painter( this );

	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(182,227,235)));
	painter.drawRect(0,0,width()*415/500, height());
	painter.setBrush(QBrush(QColor(187,209,245)));
	painter.drawRect(width()*415/500,0,width()*85/500, height());

	QString text = QString("%1").arg(_men.count());
	QFont font = painter.font();
	font.setPixelSize(height()*25/400);
	painter.setFont(font);
	painter.setPen(Qt::SolidLine);
	int textWidth = painter.fontMetrics().boundingRect(text).width();
	painter.drawText(x+w/2-textWidth/2,height()-25,text);

	painter.end();
}

void Humans::resizeEvent(QResizeEvent* event)
{
	int x = width()*415/500;
	int w = width() - x;

	int deltaX = w*0.1;
	int deltaY = w*0.25;

	int hw = (w - 3*deltaX)/2;

	int y = deltaY;
	int index = 0;
	foreach(Emotion* emotion, _emotions){
		if(index%2==0) emotion->setPosition(x + deltaX, y);
		else emotion->setPosition(x + deltaX + hw + deltaX, y);
		emotion->setRect(hw, hw);
		if(index%2==1) y += hw + deltaY;
		index++;
	}

	btnExit->setRect(height()*65/400, height()*65/400);
	btnExit->setPosition(5,height()-height()*65/400-5);

	int bw = height()*25/400;
	btnAddMan->setRect(bw ,bw);
	btnRemoveMan->setRect(bw ,bw);
	btnAddMan->setPosition(x+w*3/4-bw/2,height()-25-bw+bw/4);
	btnRemoveMan->setPosition(x+w/4-bw/2,height()-25-bw+bw/4);

	x = 0;
	double ratio = 210.0/340;
	foreach(Man* man, _men){
		man->setPosition(x,0);
		man->setRect(height()*ratio/3*2, height()/3*2);
		x+=height()*ratio/3*2 + 5;
	}
}

void Humans::onEmotionDown(){
	Emotion* em = (Emotion*) sender();

	Emotion* emotion = new Emotion(this);
	emotion->setPosition(em->x(),em->y());
	emotion->setRect(em->width(),em->height());
	emotion->setEmotion(em->type());
	emotion->update();
	emotion->setVisible(true);
	emotion->setHidden(false);
	connect(emotion, SIGNAL(mouseDown()), SLOT(onEmotionDown()));
	connect(emotion, SIGNAL(mouseUp()), SLOT(onEmotionUp()));
	connect(emotion, SIGNAL(mouseMove()), SLOT(onEmotionMove()));
	_emotions.append(emotion);

	em->raise();
}

void Humans::onEmotionUp(){
	Emotion* em = (Emotion*) sender();
	for(int i=0;i<_emotions.count();i++){
		if(_emotions.at(i)==em){
			_emotions.removeAt(i);
			i--;
		}
	}
	foreach(Man* man, _men){
		if(man->head()->isInside(this->mapFromGlobal(QCursor::pos()))){
			man->head()->applyEmotion(em);
		}
	}
	em->deleteLater();

}

void Humans::onEmotionMove(){
	Emotion* em = (Emotion*) sender();

	bool extend = false;
	foreach(Man* man, _men){
		if(man->head()->isInside(this->mapFromGlobal(QCursor::pos()))){
			extend = true;
		}
	}
	em->setExtended(extend);
}

void Humans::onAddMan()
{
	Man* man = new Man(this);
	_men.append(man);
	man->setVisible(true);
	resizeEvent(NULL);
	repaint();
}

void Humans::onRemoveMan()
{
	if(_men.count() == 0) return;

	delete _men.takeLast();
	repaint();
}
